<?php

namespace app\service;

use app\dao\CertificateDao;
use think\facade\Log;
use think\Exception;

class CertificateService
{
    // 证书数据访问对象
    private $certificateDao;

    // 构造函数，初始化证书数据访问对象
    public function __construct()
    {
        $this->certificateDao = new CertificateDao();
    }

    // 创建证书
    public function createCertificate($memberId, $type, $certificateData)
    {
        try {
            // 调用证书数据访问对象的创建证书方法
            $certificate = $this->certificateDao->createCertificate($memberId, $type, $certificateData);
            // 如果创建证书失败，抛出异常
            if (!$certificate) {
                throw new Exception('创建证书失败');
            }
            // 返回创建的证书
            return $certificate;
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error("创建证书失败: " . $e->getMessage());
            // 抛出系统异常
            throw new Exception('系统异常，请稍后再试');
        }
    }
}
